﻿using ConsoleApp_EXL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;


namespace ET
{
    public static class CreatScreens
    {
        /// <summary>
        /// 生成画面XML文件
        /// </summary>
        /// <param name="rows">源数据</param>
        /// <param name="pathOut">输出路径</param>
        /// <param name="ScreenHeight">画面高度</param>
        /// <param name="ScreenWidth">画面宽度</param>
        public static void CreateScreensFromTable(List<InputTableRow> rows, string? pathOut, ushort ScreenHeight, ushort ScreenWidth)
        {
            //计算需要创建的画面数量
            var value_in = rows.Where(row => row.Type == "Bool" & row.InOut == "In").ToList();
            var value_out = rows.Where(row => row.Type == "Bool" & row.InOut == "Out").ToList();

            int InScreenNumber;
            int OutScreenNumber;
            if (value_in.Count % 16 == 0)
            {
                InScreenNumber = value_in.Count / 16;
                OutScreenNumber = value_out.Count / 16;
            }
            else
            {
                InScreenNumber = value_in.Count / 16 + 1;
                OutScreenNumber = value_out.Count / 16 + 1;
            }

            

            //获取文件路径列表
            var fileNameListIn = Director(@"../../../Screens_IN");
            var fileNameListOut = Director(@"../../../Screens_OUT");

            //生成实体类
            List<Document> screenListIn = new List<Document>();
            List<Document> screenListOut = new List<Document>();
            var xs = new XmlSerializer(typeof(Document));

            for (int i = 0; i < InScreenNumber; i++)
            {
                using (Stream s = File.OpenRead(fileNameListIn[i]))
                {
                    Document screen = (Document)xs.Deserialize(s)!;
                    screenListIn.Add(screen);
                }
            }

            for (int i = 0; i < OutScreenNumber; i++)
            {
                using (Stream s = File.OpenRead(fileNameListOut[i]))
                {
                    Document screen = (Document)xs.Deserialize(s)!;
                    screenListOut.Add(screen);
                }
            }

            //修改画面属性
            for(int i = 0;i < screenListIn.Count;i++)
            {
                screenListIn[i].HmiScreenScreen.AttributeList.Height = ScreenHeight;
                screenListIn[i].HmiScreenScreen.AttributeList.Width = ScreenWidth;
                foreach (var iteam in screenListIn[i].HmiScreenScreen.ObjectList.HmiScreenScreenLayer)
                {
                    if (iteam.ObjectList.HmiScreenTextField != null)
                    {
                        for (int j = 0; j < 16; j++)
                        {
                            if (j+i*16< value_in.Count)
                            {
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.HmiDynamicRangeAppearanceAnimation.ObjectList.HmiDynamicTagElementTrigger.LinkList.Tag.Name = "\"" + value_in[j + i*16].Adress + "\"";
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = value_in[j + i * 16].Adress!;
                                iteam.ObjectList.HmiScreenTextField[j + 16].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = value_in[j + i * 16].Notes!;
                            }
                            else
                            {
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.HmiDynamicRangeAppearanceAnimation.ObjectList.HmiDynamicTagElementTrigger.LinkList.Tag.Name = "***";
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = "";
                                iteam.ObjectList.HmiScreenTextField[j + 16].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = "######";
                            }
                            
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }

            for (int i = 0; i < screenListOut.Count; i++)
            {
                screenListOut[i].HmiScreenScreen.AttributeList.Height = ScreenHeight;
                screenListOut[i].HmiScreenScreen.AttributeList.Width = ScreenWidth;
                foreach (var iteam in screenListOut[i].HmiScreenScreen.ObjectList.HmiScreenScreenLayer)
                {
                    if (iteam.ObjectList.HmiScreenTextField != null)
                    {
                        for (int j = 0; j < 16; j++)
                        {
                            if (j + i * 16 < value_out.Count)
                            {
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.HmiDynamicRangeAppearanceAnimation.ObjectList.HmiDynamicTagElementTrigger.LinkList.Tag.Name = "\"" + value_out[j + i * 16].Adress + "\"";
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = value_out[j + i * 16].Adress!;
                                iteam.ObjectList.HmiScreenTextField[j + 16].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = value_out[j + i * 16].Notes!;
                            }
                            else
                            {
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.HmiDynamicRangeAppearanceAnimation.ObjectList.HmiDynamicTagElementTrigger.LinkList.Tag.Name = "***";
                                iteam.ObjectList.HmiScreenTextField[j].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = "";
                                iteam.ObjectList.HmiScreenTextField[j + 16].ObjectList.MultilingualText.ObjectList.MultilingualTextItem.AttributeList.Text.body.p = "######";
                            }
                            
                        }
                    }
                }
            }

            //生成画面文件
            Directory.CreateDirectory(pathOut! + @"\Screens");
            for (int i = 0; i < screenListIn.Count; i++)
            {
                using (FileStream s = new FileStream(pathOut! + @"\Screens" + @"\画面_IN_" + i + ".xml", FileMode.Create, FileAccess.ReadWrite))
                {
                    XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, "aa") });
                    TextWriter writer = new StreamWriter(s, Encoding.UTF8);
                    xs.Serialize(writer, screenListIn[i], _namespaces);
                }
            }

            for (int i = 0; i < screenListOut.Count; i++)
            {
                using (FileStream s = new FileStream(pathOut! + @"\Screens" + @"\画面_OUT_" + i + ".xml", FileMode.Create, FileAccess.ReadWrite))
                {
                    XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, "aa") });
                    TextWriter writer = new StreamWriter(s, Encoding.UTF8);
                    xs.Serialize(writer, screenListOut[i], _namespaces);
                }
            }


        }

        public static List<string> Director(string dir)
        {
            List<string> strings = new List<string>();
            DirectoryInfo d = new DirectoryInfo(dir);
            FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
            foreach (FileSystemInfo fsinfo in fsinfos)
            {
                if (fsinfo is DirectoryInfo)     //判断是否为文件夹
                {
                    Director(fsinfo.FullName);//递归调用
                }
                else
                {
                    strings.Add(fsinfo.FullName);   
                }
            }
            return strings;
        }
    }
}
